﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using ShareContent;

namespace AncientEmpires_025_132
{
    public class PlayMenu : Menu
    {
        public enum Choice
        {
            ChoiceNew,
            ChoiceLoad,
            ChoiceSkirmish,
            ChoiceSelect,
            ChoiceBack,
            NoChoice
        }

        // Event for select main menu
        public delegate void EventHandleMenuSelect(int choice);
        public event EventHandleMenuSelect MenuSelect;

        private PlayMenuWithXML _playMenuWithXMl;
        private List<MyButton> _listMenuOption;
        private GameWindow _window;

        public List<MyButton> ListMenuOption
        {
            get { return _listMenuOption; }
            set { _listMenuOption = value; }
        }
        private Sprite2D _backGround;

        public Sprite2D BackGround
        {
            get { return _backGround; }
            set { _backGround = value; }
        }
        public PlayMenu(GameWindow window, ContentManager content, GraphicsDevice graphicsDevice, string strmenuxml)
        {
            _window = window;
            _playMenuWithXMl = content.Load<PlayMenuWithXML>(strmenuxml);
            SizeScale = _playMenuWithXMl.sizeScale;
            Size = new Vector2(graphicsDevice.Viewport.Width * SizeScale.X, graphicsDevice.Viewport.Height * SizeScale.Y);
            InitMenu(content);
        }

        public void InitMenu(ContentManager content)
        {
            ListMenuOption = new List<MyButton>();
            List<Texture2D[]> listTextureNormal = new List<Texture2D[]>();
            List<Texture2D[]> listTextureHover = new List<Texture2D[]>();
            List<Texture2D[]> listTexturePressed = new List<Texture2D[]>();
            Texture2D[] tempTexture;
            tempTexture = new Texture2D[1];
            tempTexture[0] = content.Load<Texture2D>(_playMenuWithXMl.strBackGround);
            BackGround = new Sprite2D(tempTexture, new Vector2(0, 0));
            BackGround.Size = Size;

            ListMenuOption = new List<MyButton>();
            ListMenuOption = LoadListItem(content, _playMenuWithXMl.spriteMenuXML, "PlayMenu/MenuOption/", new Vector2(0,0));

            for (int i = 0; i < ListMenuOption.Count; i++)
            {
                ListMenuOption[i].ClickButtonWithSender += new MyButton.EventHandleClickWithSender(ClickButtonWithSender);
            }

            #region oldcode
            /*
            for (int i = 0; i < _playMenuWithXMl.spriteMenuXML.Count; i++)
            {
                tempTexture = new Texture2D[1];
                tempTexture[0] = content.Load<Texture2D>("PlayMenu/MenuOption/" + _playMenuWithXMl.spriteMenuXML[i].strTexture + "_hover");
                listTextureHover.Add(tempTexture);

                tempTexture = new Texture2D[1];
                tempTexture[0] = content.Load<Texture2D>("PlayMenu/MenuOption/" + _playMenuWithXMl.spriteMenuXML[i].strTexture + "_normal");
                listTextureNormal.Add(tempTexture);

                tempTexture = new Texture2D[1];
                tempTexture[0] = content.Load<Texture2D>("PlayMenu/MenuOption/" + _playMenuWithXMl.spriteMenuXML[i].strTexture + "_pressed");
                listTexturePressed.Add(tempTexture);
            }

            Sprite2D tempSprite2DNormal = null;
            Sprite2D tempSprite2DHover = null;
            Sprite2D tempSprite2DPressed = null;
            MyButton tempMenuOption = null;
            for (int i = 0; i < listTextureNormal.Count; i++)
            {
                tempSprite2DNormal = new Sprite2D(listTextureNormal[i], new Vector2(0, 0));
                tempSprite2DNormal.NormalDelay = 8 * i + 8;
                tempSprite2DNormal.Position = _playMenuWithXMl.spriteMenuXML[i].positionBegin;
                tempSprite2DNormal.Size = _playMenuWithXMl.spriteMenuXML[i].size;
                tempSprite2DNormal.NTextures = 1;

                tempSprite2DHover = new Sprite2D(listTextureHover[i], new Vector2(0, 0));
                tempSprite2DHover.NormalDelay = 8 * i + 8;
                tempSprite2DHover.Position = _playMenuWithXMl.spriteMenuXML[i].positionBegin;
                tempSprite2DHover.Size = _playMenuWithXMl.spriteMenuXML[i].size;
                tempSprite2DHover.NTextures = 1;

                tempSprite2DPressed = new Sprite2D(listTexturePressed[i], new Vector2(0, 0));
                tempSprite2DPressed.NormalDelay = 8 * i + 8;
                tempSprite2DPressed.Position = _playMenuWithXMl.spriteMenuXML[i].positionBegin;
                tempSprite2DPressed.Size = _playMenuWithXMl.spriteMenuXML[i].size;
                tempSprite2DPressed.NTextures = 1;

                tempMenuOption = new MyButton(content, tempSprite2DNormal, tempSprite2DHover, tempSprite2DPressed);
                ListMenuOption.Add(tempMenuOption);
                ListMenuOption[i].ClickButtonWithSender += new MyButton.EventHandleClickWithSender(ClickButtonWithSender);
            }*/
            #endregion
        }

        //Process when option was clicked
        public void ClickButtonWithSender(object sender)
        {
            int choice = ListMenuOption.IndexOf((MyButton)sender);
            MenuSelect(choice);
        }

        public void UpdateMenuSize(GraphicsDevice graphicsDevice)
        {
            Size = new Vector2(graphicsDevice.Viewport.Width * SizeScale.X, graphicsDevice.Viewport.Height * SizeScale.Y);
            BackGround.Size = Size;
            for (int i = 0; i < ListMenuOption.Count; i++)
            {
                ListMenuOption[i].SetPosition(new Vector2(_playMenuWithXMl.spriteMenuXML[i].positionScale.X * Size.X, _playMenuWithXMl.spriteMenuXML[i].positionScale.Y * Size.Y));
                ListMenuOption[i].SetSize(new Vector2(_playMenuWithXMl.spriteMenuXML[i].sizeScale.X * Size.X, _playMenuWithXMl.spriteMenuXML[i].sizeScale.Y * Size.Y));
            }
        }

        public override void Update(GameTime gameTime)
        {
            BackGround.Update(gameTime);
            for (int i = 0; i < ListMenuOption.Count; i++)
            {
                ListMenuOption[i].Update(gameTime);
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            BackGround.Draw(gameTime, spriteBatch);
            for (int i = 0; i < ListMenuOption.Count; i++)
            {
                ListMenuOption[i].Draw(gameTime, spriteBatch);
            }
        }
    }
}
